package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.iface.promo.activity.enums.DiscountTypeEnum;
import com.epfly.promo.activity.dao.ActivityFoodCategoryMapper;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.entity.ActivityFoodCategoryEntity;
import com.epfly.promo.activity.feign.DataRestaurantFeignService;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 菜品分类过滤
 */
@Order(value = PredicateOrder.FOOD_CATEGORY_ORDER)
@Service
public class FoodCategoryPredicate implements ActivityDiscountPredicate {

    @Resource
    private DataRestaurantFeignService dataRestaurantFeignService;
    @Resource
    private ActivityFoodCategoryMapper activityFoodCategoryMapper;

    @Override
    public boolean test(ActivityWrapper activityWrapper) {

        ActivityEntity activityEntity = activityWrapper.getActivityEntity();

        if (activityEntity.getDiscountType().equals(DiscountTypeEnum.FOOD_CATEGORY_REBATE.getKey())) {

            List<String> foodIdList = activityWrapper.getFoodIdList();

            List<String> foodCategoryIdList = dataRestaurantFeignService.getFoodCategoryIdListByFoodIdList(foodIdList);

            ActivityFoodCategoryEntity entity = activityFoodCategoryMapper.existsByActivityIdAndFoodCategoryIdList(activityEntity.getId(), foodCategoryIdList);
            // 活动关联菜品分类中未找到用户下单的菜品分类
            return !Objects.isNull(entity);
        }

        return true;
    }
}
